package com.yjx.gulimall.product.service.impl;

import com.sun.media.jfxmedia.logging.Logger;
import com.yjx.gulimall.common.utils.R;
import com.yjx.gulimall.common.vo.product.Bounds;
import com.yjx.gulimall.common.vo.product.Skus;
import com.yjx.gulimall.product.agent.CouponAgentService;
import com.yjx.gulimall.product.service.*;
import com.yjx.gulimall.common.vo.product.BaseAttrs;
import com.yjx.gulimall.common.vo.product.SpuSaveVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.Query;

import com.yjx.gulimall.product.dao.SpuInfoDao;
import com.yjx.gulimall.product.entity.SpuInfoEntity;
import org.springframework.transaction.annotation.Transactional;


@Service("spuInfoService")
@Slf4j
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private SpuImagesService spuImagesService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    @Autowired
    private CouponAgentService couponAgentService;

    @Autowired
    private SkuInfoService skuInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveSpuInfo(SpuSaveVO spuSaveVO) {

        // 1.保存spu基本信息 pms_spu_info

        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuSaveVO,spuInfoEntity);
        spuInfoEntity.setUpdateTime(LocalDateTime.now());
        spuInfoEntity.setCreateTime(LocalDateTime.now());
        baseMapper.insert(spuInfoEntity);
        Long spuId = spuInfoEntity.getId();

        // 2.保存spu描述图片（商品介绍里面的图） pms_spu_info_desc

        List<String> decript = spuSaveVO.getDecript();
        spuInfoDescService.saveBaseInfoDesc(spuId,decript);

        // 3.保存spu图片集（商品展示图） pms_spu_images
        List<String> images = spuSaveVO.getImages();
        spuImagesService.saveBaseImage(spuId,images);


        // 4.保存spu基本参数值 pms_product_attr_value
        List<BaseAttrs> baseAttrs = spuSaveVO.getBaseAttrs();
        productAttrValueService.saveProductAttrValue(spuId,baseAttrs);

        // 5.保存spu的积分信息（购买产生积分，现阶段绑定spu，可以绑定sku）
        Bounds bounds = spuSaveVO.getBounds();
        R r = couponAgentService.saveSpuBounds(spuId,bounds);
        if(r.getCode() != 0){
            log.error("SpuInfoServiceImpl远程调用saveSpuBounds 保存失败");
        }
        // 6.保存当前spu对应的所有sku信息
        //   6.1)sku的基本信息：pms_sku_info
        //   6.2)sku的图片信息：pms_sku_images
        //   6.3)sku的销售属性值：pms_sku_sale_attr_value
        //   6.4)sku的打折（买几件打几折）、满减信息（满多少减多少）、会员价格：
        //   sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        List<Skus> skus = spuSaveVO.getSkus();
        skuInfoService.saveSkuInfo(spuInfoEntity,skus);

    }

}